//
//  HPasswordViewController.m
//  Router
//
//  Created by Zhen Huang on 2018/6/19.
//  Copyright © 2018年 James. All rights reserved.
//

#import "HPasswordViewController.h"
#import "HHomeViewController.h"
#import "AppDelegate.h"
@interface HPasswordViewController ()
<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *passWordText;
@property (nonatomic, strong) UITextField *makeSurePassWord;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, copy) NSString *userName;
@property (nonatomic, copy) NSString *mobile;
@property (nonatomic, copy) NSString *code;
@end

@implementation HPasswordViewController
- (instancetype)initWithUserName:(NSString *)userName
                          mobile:(NSString *)mobile
                            code:(NSString *)code{
    if (self) {
        self.userName = userName;
        self.mobile = mobile;
        self.code = code;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChangeEditingNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
    [self.view addSubview:self.passWordText];
    [self.view addSubview:self.makeSurePassWord];
    [self.view addSubview:self.registerButton];
    [self createUserCodeAndPassWord];
}
- (void)UITextFieldTextDidChangeEditingNotification:(NSNotification *)noti {
    UITextField *textField = noti.object;
    if (textField.tag == 400) {
        self.passWordText.text = textField.text;
    }else if (textField.tag == 500) {
        self.makeSurePassWord.text = textField.text;
    }
}
- (UITextField *)passWordText {
    if (!_passWordText) {
        _passWordText = [[UITextField alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2 - 15.5 - 25, ScreenWidth - 200, 25)];
        _passWordText.delegate = self;
        _passWordText.placeholder = @"请输入密码";
        _passWordText.textColor = kTitleBackGroundColor;
        _passWordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_passWordText.placeholder attributes:@{NSForegroundColorAttributeName: [Networking colorWithHexString:@"#455A6A"]}];
        _passWordText.tag = 400;
    }
    return _passWordText;
}
- (UITextField *)makeSurePassWord {
    if (!_makeSurePassWord) {
        _makeSurePassWord = [[UITextField alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2 + 56 - 15.5 - 25, ScreenWidth - 200, 25)];
        _makeSurePassWord.delegate = self;
        _makeSurePassWord.placeholder = @"确认密码";
        _makeSurePassWord.textColor = kTitleBackGroundColor;
        _makeSurePassWord.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_makeSurePassWord.placeholder attributes:@{NSForegroundColorAttributeName:[Networking colorWithHexString:@"#455A6A"]}];
        _makeSurePassWord.tag = 500;
    }
    return _makeSurePassWord;
}
- (void)createUserCodeAndPassWord {
    for (int i = 0; i < 2; i ++) {
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(20, ScreenHeight / 2  + 56 *i, ScreenWidth - 40, 1)];
        lineView.backgroundColor = [Networking colorWithHexString:@"#455A6A"];
        [self.view addSubview:lineView];
    }
}
- (UIButton *)registerButton {
    if (!_registerButton) {
        _registerButton = [[UIButton alloc]initWithFrame:CGRectMake(28,ScreenHeight - 150,ScreenWidth - 56,55)];
        _registerButton.layer.masksToBounds = YES;
        _registerButton.layer.cornerRadius = 25;
        if (iPhone5) {
            _registerButton.height = 40;
            _registerButton.layer.cornerRadius = 20;
        }
        _registerButton.titleLabel.font = [UIFont fontWithName:PingFangSCRegular size:16];
        _registerButton.backgroundColor = kNavBlueColor;
        [_registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_registerButton setTitle:@"立即注册" forState:UIControlStateNormal];
        [[_registerButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
            NSString *url = F(@"%@/users", BaseUrl);
            NSDictionary *paramDic = @{@"username":self.userName?:@"",
                                       @"mobile":self.mobile?:@"",
                                       @"code":self.code?:@"",
                                       @"password":self.passWordText.text?:@""
                                       };
            NSString *param = [self dictionaryToJson:paramDic];
            if (self.passWordText.text.length < 6 || self.passWordText.text.length > 22) {
                [self alert:@"密码不能低于6位且不超过22位"];
                return ;
            }else if (![self.passWordText.text isEqualToString:self.makeSurePassWord.text]) {
                [self alert:@"两次密码输入不一致"];
                return;
            }else {
                [self receivePOSTWithTokenURLString:url method:@"POST" body:param Token:nil Blocks:^(NSDictionary *tempData, NSInteger state) {
                    if (tempData) {
                        [[TMCache sharedCache]setObject:[tempData objectForKey:@"token"] forKey:@"Token"];
                        [[TMCache sharedCache]setObject:[[tempData objectForKey:@"user"]objectForKey:@"username"] forKey:@"UserName"];
                        AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
                        [app rootViewToTabBar];
                    }
                }];
            }
        }];
    }
    return _registerButton;
}
//点击界面的任意地方，回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
//回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
